package com.example.forum.service.impl;

import com.example.forum.common.ResultCode;
import com.example.forum.dao.MessageMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Message;
import com.example.forum.service.IMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {


    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Integer addMessage(Long receiveUserId,String content,Long userId) {
        Message message = new Message();
        message.setContent(content);
        message.setReceive_user_id(receiveUserId);
        message.setPost_user_id(userId);
        //设置默认值
        message.setState(0);
        message.setDelete_state(0);
        message.setCreate_time(new Date(System.currentTimeMillis()));
        message.setUpdate_time(new Date(System.currentTimeMillis()));

        Integer ret = messageMapper.insert(message);
        if(ret == null || ret <= 0) {
            log.warn("发送信息失败");
            throw new ApplicationException("发送信息失败");
        }

        return ret;

    }

    @Override
    public List<Message> selectMessageByUserId(Long userId) {
        List<Message> messages = messageMapper.selectMessageByUserId(userId);
        log.info("返回用户接收信成功");
        return messages;
    }

    @Override
    public Integer updateMessageStatus(Long id) {
        if(id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
        }

        Message message = messageMapper.selectByPrimaryKey(id);
        if(message == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }

        message.setState(1);
        Integer ret = messageMapper.updateByPrimaryKeySelective(message);
        if(ret == null || ret <= 0) {
            log.warn("读取信息失败");
            throw new ApplicationException("读取信息失败");
        }
        log.info("读取信息成功");
        return ret;
    }

    @Override
    public Integer getNotReadCount() {
        List<Message> lists = messageMapper.selectAllMessage();
        log.info("获取成功");
        return lists.size();
    }

    public Integer addReplyMessage(Long id,String content) {
        Message message = messageMapper.selectByPrimaryKey(id);
        if(message == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }

        Message replyMessage = new Message();
        replyMessage.setState(0);
        replyMessage.setReceive_user_id(message.getPost_user_id());
        replyMessage.setPost_user_id(message.getReceive_user_id());
        replyMessage.setContent(content);
        replyMessage.setDelete_state(0);
        replyMessage.setCreate_time(new Date(System.currentTimeMillis()));
        replyMessage.setUpdate_time(new Date(System.currentTimeMillis()));

        Integer ret = messageMapper.insert(replyMessage);
        if(ret == null || ret <= 0) {
            log.warn("回复信息失败");
            throw new ApplicationException("回复信息失败");
        }
        log.info("回复信息成功");
        return ret;
    }

}
